package com.cyh.ebptbatchwar.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

//表示这个类为一个配置类
@Configuration
public class DataSourceConfig {
    @Value("")
//    @Bean("dynamicDataSource")
//    public DataSource dynamicDataSource() {
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        Map<Object, Object> dataSourceMap = new HashMap<>(2);
//        dataSourceMap.put("master", master());
//        dataSourceMap.put("slave", slave());
//        // 将 master 数据源作为默认指定的数据源
//        dynamicDataSource.setDefaultDataSource(master());
//        // 将 master 和 slave 数据源作为指定的数据源
//        dynamicDataSource.setDataSources(dataSourceMap);
//        return dynamicDataSource;
//    }


//    @Bean
//    public PlatformTransactionManager transactionManager() {
//        // 配置事务管理, 使用事务时在方法头部添加@Transactional注解即可
//        return new DataSourceTransactionManager(dynamicDataSource());
//    }

    // 将这个对象放入Spring容器中
    @Bean(name = "springBatchDataSource")
    @Qualifier("springBatchDataSource")
    public DataSource springBatchDataSource() {
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2)
                .build();
    }
    // ---------------------------------------------------------

    // 将这个对象放入Spring容器中
    @Bean(name = "dataSource1")
    // 表示这个数据源是默认数据源
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.druid.master")
//    @Qualifier("dataSource1")
    public DataSource dataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "dataSource1JdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("dataSource1") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    @Bean
    public PlatformTransactionManager getTransactionManager() {
        return new ResourcelessTransactionManager();
    }


    /**
     * 动态数据源
     * @param springBatchDataSource
     *  数据源1，内存数据源
     * @param dataSource1
     *  数据源2，业务数据源
     * @return 数据源
     */
    @Primary
    @Bean("dynamic")
    @DependsOn({"springBatchDataSource","dataSource1"})
    public DataSource dataSource(@Qualifier("springBatchDataSource") DataSource springBatchDataSource,@Qualifier("dataSource1") DataSource dataSource1){
        return DruidDataSourceBuilder.create().build();
    }
//    @Bean
//    public JobRepository getJobRepo() throws Exception {
//        return new MapJobRepositoryFactoryBean(getTransactionManager()).getObject();
//    }

//    @Bean
//    public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {
//        SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
//        simpleJobLauncher.setJobRepository(jobRepository);
//        return simpleJobLauncher;
//    }

}
